//
//  SelectAddressView.swift
//  swiftProject
//
//  Created by ZhangSai on 2021/7/8.
//

import UIKit

class SelectAddressView: UIView,UIPickerViewDelegate,UIPickerViewDataSource {

    typealias SelectAddressBlock = (_ selectCityModel:WLCityModel) -> Void
    var selectAddressBlock : SelectAddressBlock!
    var selectCityModel : WLCityModel!
    var indexOfProvince : Int!//当前默认选中的省份
    var lastPath : IndexPath!// 单选
    var isRepeatRolling : Bool!// 是否重复滚动
    var dataSource : Array<WLAreaModel>!

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.layer.masksToBounds = true
        self.layer.cornerRadius = 10
        if dataSource.count > 0 {
            selectCityModel = dataSource[0].region?[0]
        }
        self.setUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUI()  {
        self.backgroundColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.5)
        self.addSubview(self.bgView)
        bgView.addSubview(self.headerView)
        bgView.addSubview(self.pickerView)
        bgView.addSubview(self.confirmBtn)
        bgView.snp.makeConstraints { (make) in
            make.left.equalTo(self.snp.right).offset(0);
            make.bottom.equalTo(self.snp.bottom).offset(0);
            make.height.equalTo(Constants.ratioIntervalHeighrt * 517);
        }
        headerView.snp.makeConstraints { (make) in
            make.left.equalTo(self.snp.left).offset(0);
            make.right.equalTo(self.snp.right).offset(0);
            make.top.equalTo(self.bgView.snp.top).offset(0);
            make.height.equalTo(200);
        }
        confirmBtn.snp.makeConstraints { (make) in
            make.left.equalTo(self.snp.left).offset(60);
            make.right.equalTo(self.snp.right).offset(-60);
            make.bottom.equalTo(self.snp.bottom).offset(-(Constants.xSafeBottomHeight + 10));
            make.height.equalTo(40);
        }
        pickerView.snp.makeConstraints { (make) in
            make.left.equalTo(self.snp.left).offset(0);
            make.width.equalTo(Constants.screenWidth);
            make.bottom.equalTo(self.confirmBtn.snp.top).offset(-70);
            make.top.equalTo(self.headerView.snp.bottom).offset(0);
        }
    }
    
    @objc func clickOffBtn()  {
        super.removeFromSuperview()
    }
    
    @objc func clickConfimBtn()  {
        self.removeFromSuperview()
        if (selectAddressBlock != nil) {
            selectAddressBlock(selectCityModel)
        }
    }
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 2

    }
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        let areaModel:WLAreaModel = dataSource[indexOfProvince]
        if component == 0 {
            return dataSource.count
        }
        return areaModel.region?.count ?? 0
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if component == 0 {
            let areaModel:WLAreaModel = dataSource[row]
            return areaModel.name
        }
        let areaModel2:WLAreaModel = dataSource[indexOfProvince]
        let cityModel:WLCityModel = (areaModel2.region?[row])!
        return cityModel.name
    }
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        let areaModel:WLAreaModel;
        if component == 0 {// 第一列省选中后，就要更新第二列的数据
            // 更新选中省份的索引
            self.indexOfProvince = row
            pickerView.reloadComponent(1)
            areaModel = dataSource[indexOfProvince]
            pickerView.selectRow(0, inComponent: 1, animated: true)
            selectCityModel = areaModel.region?[0]
        } else {
            areaModel = dataSource[indexOfProvince]
            selectCityModel = areaModel.region?[row]
        }
        
    }
    
    lazy var headerView: UIView = {
        let headerView = UIView.init()
        headerView.layer.cornerRadius = 15
        headerView.layer.masksToBounds = true
        headerView.backgroundColor = UIColor.white
        
        let btn = UIButton.init(type: .custom)
        btn.setImage(UIImage.init(named: "login_cancel"), for: .normal)
        btn.addTarget(self, action: #selector(clickOffBtn), for: .touchUpInside)
        headerView.addSubview(btn)
        
        let lab = UILabel.init()
        lab.text = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_selectArea)
        headerView.addSubview(lab)
        lab.textColor = UIColor.init(hexString: "#3A3A3A")
        lab.font = UIFont.systemFont(ofSize: 19)
        lab.textAlignment = .center
        lab.snp.makeConstraints { (make) in
            make.height.equalTo(25);
            make.centerX.equalTo(self.headerView.snp.centerX).offset(0);
            make.top.equalTo(self.headerView.snp.top).offset(15);
        }
        btn.snp.makeConstraints { (make) in
            make.right.equalTo(self.headerView.snp.right).offset(-15);
            make.height.equalTo(13);
            make.width.equalTo(13);
            make.centerY.equalTo(lab.snp.centerY);
        }
        let lineView = UIView()
        headerView.addSubview(lineView)
        lineView.backgroundColor = UIColor.init(hexString: "#D8D8D8")
        lineView.snp.makeConstraints { (make) in
            make.height.equalTo(1);
            make.right.equalTo(self.headerView.snp.right).offset(0);
            make.left.equalTo(self.headerView.snp.left).offset(-0);
            make.top.equalTo(lab.snp.bottom).offset(15);
        }
        let lineView2 = UIView.init()
        headerView.addSubview(lineView2)
        lineView2.backgroundColor = UIColor.init(hexString: "#D8D8D8")
        lineView2.snp.makeConstraints { (make) in
            make.height.equalTo(1);
            make.right.equalTo(self.headerView.snp.right).offset(0);
            make.left.equalTo(self.headerView.snp.left).offset(-0);
            make.top.equalTo(lineView.snp.bottom).offset(54);
        }
        let cityLab = UILabel.init()
        cityLab.text = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_city)
        cityLab.font = UIFont.boldSystemFont(ofSize: 17)
        cityLab.textAlignment = .center
        headerView.addSubview(cityLab)
        cityLab.snp.makeConstraints { (make) in
            make.height.equalTo(24);
            make.width.equalTo(Constants.screenWidth/2);
            make.top.equalTo(lineView.snp.bottom).offset(15);
            make.left.equalTo(self.headerView.snp.left).offset(0);
        }
        let addressLab = UILabel.init()
        addressLab.text = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_area)
        addressLab.font = UIFont.boldSystemFont(ofSize: 17)
        addressLab.textAlignment = .center
        headerView.addSubview(addressLab)
        addressLab.snp.makeConstraints { (make) in
            make.height.equalTo(24);
            make.width.equalTo(Constants.screenWidth/2);
            make.top.equalTo(lineView.snp.bottom).offset(15);
            make.left.equalTo(cityLab.snp.right).offset(0);
        }
        return headerView
    }()
    lazy var confirmBtn: UIButton = {
        let confirmBtn = UIButton.init(type: .custom)
        confirmBtn.setTitle(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_confirm), for: .normal)
        confirmBtn.setTitleColor(UIColor.init(hexString: "#000000"), for: .normal)
        confirmBtn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
        confirmBtn.layer.cornerRadius = 20
        confirmBtn.backgroundColor = UIColor.init(hexString: "#FFD10A")
        confirmBtn.addTarget(self, action: #selector(clickConfimBtn), for: .touchUpInside)
        return confirmBtn
    }()
    
    lazy var bgView: UIView = {
        let bgView = UIView()
        bgView.backgroundColor = UIColor.white
        return bgView
    }()
    
    lazy var pickerView: UIPickerView = {
        let pickerView = UIPickerView.init()
        pickerView.delegate = self
        pickerView.dataSource = self
        return pickerView
    }()

}
